/*
 * Created by JFormDesigner on Wed May 07 09:22:25 CST 2025
 */

package com.soft.bams.server;

import com.soft.bams.client.ATMClient;
import com.soft.bams.service.Bank;
import com.soft.bams.vo.RankVO;

import java.awt.*;
import java.awt.event.*;
import java.util.Map;
import java.util.Set;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 21603
 */
public class RankPanel extends JPanel {
    private DefaultTableModel tableModel;

    public RankPanel() {
        initComponents();
        tableModel = new DefaultTableModel();
    }

    private void getAccountsTotalCeiling(ActionEvent e) {
        double totalCeiling = Bank.getInstance().getTotalCeiling();
        String[] tableTitle = {"编号","所有账户透支额度总额"};
        tableModel = new DefaultTableModel(tableTitle, 0);
        tableModel.addRow(new Object[]{1,totalCeiling});
        table.setModel(tableModel);
    }

    private void getAccountsTotalBalance(ActionEvent e) {
        double totalBalance = Bank.getInstance().getTotalBalance();
        String[] tableTitle = {"编号","所有账户余额总额"};
        tableModel = new DefaultTableModel(tableTitle, 0);
        tableModel.addRow(new Object[]{1,totalBalance});
        table.setModel(tableModel);
    }

    private void getAccountsTotalLoan(ActionEvent e) {
        double totalLoan = Bank.getInstance().getTotalLoan();
        String[] tableTitle = {"编号","所有账户贷款总额"};
        tableModel = new DefaultTableModel(tableTitle, 0);
        tableModel.addRow(new Object[]{1,totalLoan});
        table.setModel(tableModel);
    }

    private void getAccountsAssetRank(ActionEvent e) {
        Set<RankVO> rankVOSet = Bank.getInstance().printAssetRank();
        String[] tableTitle = {"编号","personId","total"};
        tableModel = new DefaultTableModel(tableTitle, 0);
        int rank = 1;
        for (RankVO rankVO : rankVOSet) {
            tableModel.addRow(new Object[]{rank++,rankVO.getPersonId(),rankVO.getTotal()});
        }
        table.setModel(tableModel);
    }

    private void back(ActionEvent e) {
        CardLayout cardLayout  = (CardLayout) this.getParent().getLayout();
        cardLayout.show(this.getParent(),"server");
    }

    private void getAccountsIpInfo(ActionEvent e) {
        Set<Map.Entry<Long, String>> loggedAccounts = Bank.getInstance().getLoggedAccountsInfo();
        String[] tableTitle = {"编号", "账户ID", "IP地址"};
        tableModel = new DefaultTableModel(tableTitle, 0);

        int rank = 1;
        for (Map.Entry<Long, String> entry : loggedAccounts) {
            tableModel.addRow(new Object[]{rank++, entry.getKey(), entry.getValue()});
        }
        table.setModel(tableModel);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        table = new JTable();
        getAccountsTotalCeilingButton = new JButton();
        getAccountsTotalLoanButton = new JButton();
        getAccountsAssetRankButton = new JButton();
        backButton = new JButton();
        getAccountsTotalBalanceButton = new JButton();
        getAccountsIpInfoButton = new JButton();

        //======== this ========
        setLayout(null);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table);
        }
        add(scrollPane1);
        scrollPane1.setBounds(100, 130, 620, 365);

        //---- getAccountsTotalCeilingButton ----
        getAccountsTotalCeilingButton.setText("\u83b7\u53d6\u900f\u652f\u989d\u5ea6\u603b\u989d");
        getAccountsTotalCeilingButton.addActionListener(e -> getAccountsTotalCeiling(e));
        add(getAccountsTotalCeilingButton);
        getAccountsTotalCeilingButton.setBounds(250, 30, 150, getAccountsTotalCeilingButton.getPreferredSize().height);

        //---- getAccountsTotalLoanButton ----
        getAccountsTotalLoanButton.setText("\u83b7\u53d6\u8d37\u6b3e\u603b\u989d");
        getAccountsTotalLoanButton.addActionListener(e -> getAccountsTotalLoan(e));
        add(getAccountsTotalLoanButton);
        getAccountsTotalLoanButton.setBounds(430, 30, 150, getAccountsTotalLoanButton.getPreferredSize().height);

        //---- getAccountsAssetRankButton ----
        getAccountsAssetRankButton.setText("\u83b7\u53d6\u603b\u8d44\u4ea7\u6392\u540d");
        getAccountsAssetRankButton.addActionListener(e -> getAccountsAssetRank(e));
        add(getAccountsAssetRankButton);
        getAccountsAssetRankButton.setBounds(605, 30, 150, getAccountsAssetRankButton.getPreferredSize().height);

        //---- backButton ----
        backButton.setText("\u8fd4\u56de");
        backButton.addActionListener(e -> back(e));
        add(backButton);
        backButton.setBounds(new Rectangle(new Point(365, 505), backButton.getPreferredSize()));

        //---- getAccountsTotalBalanceButton ----
        getAccountsTotalBalanceButton.setText("\u83b7\u53d6\u4f59\u989d\u603b\u989d");
        getAccountsTotalBalanceButton.addActionListener(e -> getAccountsTotalBalance(e));
        add(getAccountsTotalBalanceButton);
        getAccountsTotalBalanceButton.setBounds(70, 30, 150, getAccountsTotalBalanceButton.getPreferredSize().height);

        //---- getAccountsIpInfoButton ----
        getAccountsIpInfoButton.setText("\u83b7\u53d6IP\u4fe1\u606f");
        getAccountsIpInfoButton.addActionListener(e -> getAccountsIpInfo(e));
        add(getAccountsIpInfoButton);
        getAccountsIpInfoButton.setBounds(335, 80, 150, getAccountsIpInfoButton.getPreferredSize().height);

        setPreferredSize(new Dimension(800, 600));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable table;
    private JButton getAccountsTotalCeilingButton;
    private JButton getAccountsTotalLoanButton;
    private JButton getAccountsAssetRankButton;
    private JButton backButton;
    private JButton getAccountsTotalBalanceButton;
    private JButton getAccountsIpInfoButton;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
